package com.SouthernWall_404.age_tool.common.world.block.RedStoneComponent;

import com.SouthernWall_404.age_tool.common.API.Capability.TriggersCap;
import com.SouthernWall_404.age_tool.common.init.ModBlockEntities;
import com.SouthernWall_404.age_tool.common.world.Entity.ExcuteBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.gameevent.GameEventListener;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

public class ExcuteBlock extends FaceAttachedHorizontalDirectionalBlock implements EntityBlock {

    public static final BooleanProperty POWERED;

    static {
        POWERED = BlockStateProperties.POWERED;
    }

    private boolean isTriggered=false;

    public int getSignal(BlockState p_54635_, BlockGetter p_54636_, BlockPos p_54637_, Direction p_54638_) {
        return (Boolean)p_54635_.getValue(POWERED) ? 15 : 0;
    }

    public int getDirectSignal(BlockState p_54670_, BlockGetter p_54671_, BlockPos p_54672_, Direction p_54673_) {
        return (Boolean)p_54670_.getValue(POWERED) && getConnectedDirection(p_54670_) == p_54673_ ? 15 : 0;
    }

    public boolean isSignalSource(BlockState p_54675_) {
        return true;
    }

    public ExcuteBlock(Properties p_55678_) {
        super(p_55678_);
        this.registerDefaultState(this.stateDefinition.any()
                .setValue(FACING, Direction.NORTH)
                .setValue(FACE, AttachFace.WALL)
                .setValue(POWERED, false));

    }

    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> p_54663_) {
        p_54663_.add(new Property[]{FACE, FACING, POWERED});
    }

    @Override
    public @Nullable BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
        return new ExcuteBlockEntity(blockPos,blockState);
    }

    @Override
    public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult p_60508_) {

        BlockEntity blockEntity=level.getBlockEntity(blockPos);
        if(blockEntity instanceof ExcuteBlockEntity excuteBlockEntity)
        {
            TriggersCap triggersCap=excuteBlockEntity.getTriggers();
            if(triggersCap!=null)
            {
                player.sendSystemMessage(Component.literal(""+triggersCap.isActive(excuteBlockEntity.remoteId)));
            }
        }


        return super.use(blockState, level, blockPos, player, hand, p_60508_);
    }

    @Override
    public @Nullable <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state1, BlockEntityType<T> type) {

        if (type != ModBlockEntities.EXCUTER_BLOCK_ENTITY.get()) {
            return null;
        }

        // 只在服务器端执行
        if (level.isClientSide) {
            return null;
        }

        // 返回每 tick 都会执行的 ticker
        return (lvl, pos, st, blockEntity) -> {
            if (blockEntity instanceof ExcuteBlockEntity excuteBlockEntity) {
                TriggersCap triggersCap=excuteBlockEntity.getTriggers();
                if(excuteBlockEntity.isTriggered!=triggersCap.isActive(excuteBlockEntity.remoteId))
                {
                    excuteBlockEntity.isTriggered=triggersCap.isActive(excuteBlockEntity.remoteId);
                    if (lvl.isClientSide) {
                        BlockState $$6 = (BlockState)st.cycle(POWERED);
                        if ((Boolean)$$6.getValue(POWERED)) {
                        }

                        return;
                    } else {
                        BlockState $$7 = this.pull(st,lvl,pos);
                        float $$8 = (Boolean)$$7.getValue(POWERED) ? 0.6F : 0.5F;
                    }
                }

            }
        };
    }

    @Override
    public @Nullable <T extends BlockEntity> GameEventListener getListener(ServerLevel p_221121_, T p_221122_) {
        return EntityBlock.super.getListener(p_221121_, p_221122_);
    }

    public BlockState pull(BlockState blockState, Level level, BlockPos blockPos) {
        blockState = (BlockState)blockState.cycle(POWERED);
        level.setBlock(blockPos, blockState, 3);

        this.updateNeighbours(blockState, level, blockPos);
        return blockState;
    }

    private void updateNeighbours(BlockState p_54681_, Level p_54682_, BlockPos p_54683_) {
        p_54682_.updateNeighborsAt(p_54683_, this);
        p_54682_.updateNeighborsAt(p_54683_.relative(getConnectedDirection(p_54681_).getOpposite()), this);
    }

}
